﻿Imports System.IO
Imports System.Data.SqlClient
Public Class frmstaffeditor


    Private Sub frmstaffeditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cbo_position.ValueMember = "POS_ID"
        cbo_position.DisplayMember = "POS_NAME"
        cbo_position.DataSource = CMD.ExecuteDataTable("select * from TBL_POSITION")
        If txtid.Text.Trim = "" Then
            rdbMale.Checked = True
            txtemp_code.Text = generatenum.GenerateNo()
        Else
            CMD.Parameters("@ID") = txtid.Text
            Dim drs = CMD.ExecuteDataReader("select * from TBL_EMPLOYEE WHERE EMP_ID=@ID")
            While drs.Read
                txtemp_code.Text = drs(1)
                txtemp_name.Text = drs(2)

                Dim data() As Byte = DirectCast(drs(3), Byte())
                Dim fs As New MemoryStream(data, True)
                Dim bmp As New Bitmap(fs)
                PictureBox1.Image = bmp
                Dim sex = drs(4)
                If sex = "F" Then
                    rdbFmale.Checked = True
                Else
                    rdbMale.Checked = True
                End If
                cbo_position.SelectedValue = drs(5)
                dtpdob.Text = drs(6)
                txtadr.Text = drs(7)
                txtphone.Text = drs(8)
                txtsalary.Text = drs(9)
            End While
            drs.Close()
        End If
       
    End Sub

    Private Sub btnbrowse_Click(sender As Object, e As EventArgs) Handles btnbrowse.Click
        If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
            txtpathfile.Text = OpenFileDialog1.FileName
            PictureBox1.ImageLocation = txtpathfile.Text
        End If
    End Sub

    Private Sub cbo_position_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_position.SelectedIndexChanged

    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        'check_val()
        If txtid.Text.Trim = "" Then
            Dim fs As New FileStream(txtpathfile.Text, FileMode.Open, FileAccess.Read)
            Dim data() As Byte = New Byte(fs.Length - 1) {}
            fs.Read(data, 0, data.Length)
            Dim sex As String
            If rdbFmale.Checked = True Then
                sex = "F"
            Else
                sex = "M"
            End If
            CMD.Parameters("@name") = txtemp_name.Text.ToString
            CMD.Parameters("@code") = txtemp_code.Text.ToString
            Dim count = CMD.ExecuteScalar("Select count(*) from TBL_EMPLOYEE WHERE EMP_NAME=@name AND EMP_CODE=@code")
            If count > 0 Then
                MsgBox(" Your Name And password is alread Exist... Please Choose another Name")
            Else
                CMD.Parameters("@code") = txtemp_code.Text.ToString
                CMD.Parameters("@name") = txtemp_name.Text.ToString

                CMD.Parameters("@image") = data
                CMD.Parameters("@sex") = sex
                CMD.Parameters("@position") = cbo_position.SelectedValue
                CMD.Parameters("@dob") = dtpdob.Text.ToString
                CMD.Parameters("@adr") = txtadr.Text.ToString
                CMD.Parameters("@phone") = txtphone.Text.ToString
                CMD.Parameters("@salary") = txtsalary.Text
                CMD.ExecuteNonQuery("INSERT INTO TBL_EMPLOYEE VALUES(@code,@name,@image,@sex,@position,@dob,@adr,@phone,@salary)")
                MsgBox("One record Insert Successfull.....!")
                clear()
                Form1.loademployee()
            End If
        Else
            If txtpathfile.Text.Trim = "" Then
                Dim sex As String
                If rdbFmale.Checked = True Then
                    sex = "F"
                Else
                    sex = "M"
                End If
                CMD.Parameters("@EMP_ID") = Me.txtid.Text
                CMD.Parameters("@code") = txtemp_code.Text.ToString
                CMD.Parameters("@name") = txtemp_name.Text.ToString
                CMD.Parameters("@sex") = sex
                CMD.Parameters("@position") = cbo_position.SelectedValue
                CMD.Parameters("@dob") = dtpdob.Text.ToString
                CMD.Parameters("@adr") = txtadr.Text.ToString
                CMD.Parameters("@phone") = txtphone.Text.ToString
                CMD.Parameters("@salary") = txtsalary.Text
                CMD.ExecuteNonQuery("UPDATE TBL_EMPLOYEE SET EMP_CODE=@code,EMP_NAME=@name,EMP_SEX=@sex,POS_ID=@position,DOB=@dob,EMP_ADR=@adr,EMP_PHONE=@phone,SALARY=@salary WHERE EMP_ID=@EMP_ID")
                MsgBox("One record Update Successfull.....!")
                clear()
                Form1.loademployee()
            Else
                Dim fs As New FileStream(txtpathfile.Text, FileMode.Open, FileAccess.Read)
                Dim data() As Byte = New Byte(fs.Length - 1) {}
                fs.Read(data, 0, data.Length)
                Dim sex As String
                If rdbFmale.Checked = True Then
                    sex = "F"
                Else
                    sex = "M"
                End If
                CMD.Parameters("@EMP_ID") = Me.txtid.Text
                CMD.Parameters("@code") = txtemp_code.Text.ToString
                CMD.Parameters("@name") = txtemp_name.Text.ToString
                CMD.Parameters("@sex") = sex
                CMD.Parameters("@image") = data
                CMD.Parameters("@position") = cbo_position.SelectedValue
                CMD.Parameters("@dob") = dtpdob.Text.ToString
                CMD.Parameters("@adr") = txtadr.Text.ToString
                CMD.Parameters("@phone") = txtphone.Text.ToString
                CMD.Parameters("@salary") = txtsalary.Text
                CMD.ExecuteNonQuery("UPDATE TBL_EMPLOYEE SET EMP_CODE=@code,EMP_NAME=@name,IMAGE=@image,EMP_SEX=@sex,POS_ID=@position,DOB=@dob,EMP_ADR=@adr,EMP_PHONE=@phone,SALARY=@salary WHERE EMP_ID=@EMP_ID")
                MsgBox("One record Update Successfull.....!")
                clear()
                Form1.loademployee()
            End If
        End If
    End Sub
    Public Sub check_val()
        If txtpathfile.Text.Trim = "" Then
            MsgBox("Please choose an image ....!")
            Exit Sub
        End If

        If txtemp_name.Text.Trim = "" Then
            MsgBox("Please Input Employee Name ....!")
            txtemp_name.Focus()
            Exit Sub
        End If
       
        If txtsalary.Text.Trim = "" Then
            MsgBox("Please Input Employee salary ....!")
            txtsalary.Focus()
            Exit Sub
        End If
        If txtadr.Text.Trim = "" Then
            MsgBox("Please Input Employee address ....!")
            txtadr.Focus()
            Exit Sub
        End If

        If txtphone.Text.Trim = "" Then
            MsgBox("Please Input Employee Phone ....!")
            txtphone.Focus()
            Exit Sub
        End If
        If validator.IsPhone(txtphone.Text) = False Then
            MsgBox("Please check your phone number EX :098 765 432....!")
            Exit Sub
        End If
    End Sub
    Public Sub clear()
        txtemp_name.Clear()
        txtphone.Clear()
        txtsalary.Clear()
        txtpathfile.Clear()
        txtadr.Clear()
        PictureBox1.ImageLocation = ""
    End Sub
    Private Sub txtsalary_TextChanged(sender As Object, e As EventArgs) Handles txtsalary.TextChanged
        If IsNumeric(txtsalary.Text) Or txtsalary.Text.Trim = "" Then
        Else
            MsgBox("Please input Number only...!")
            txtsalary.SelectAll()
        End If
    End Sub

    Private Sub txtphone_TextChanged(sender As Object, e As EventArgs) Handles txtphone.TextChanged

    End Sub
End Class